import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.aliasi.classify.JointClassification;

class MyJointClassification extends JointClassification {
	 
	 static double calibrationConstant = Math.pow(10,27);
	 static double sigmoidConstant = 1- Math.pow(10, -27);
	 
     public MyJointClassification(String[] categories, double[] log2JointProbs) { super(categories, log2JointProbs); };

	 public double toScore ( double score ) {
		 return 1.0 / (1.0 + Math.exp((-2 * calibrationConstant * score) + 2.0)); 
	 }  
	 
	 public double toScoreSigmoid(double score){
		 double constant = -51;
	    	BigDecimal k = new BigDecimal(-1.0).subtract(new BigDecimal(Math.pow(10.0,constant)));
	    	BigDecimal v2 = k.subtract(new BigDecimal(score)).add(new BigDecimal(1.0));
	    	BigDecimal v1 = k.multiply(new BigDecimal(score));
	    	return v1.divide(v2,1).doubleValue();
	 }
	 
	 public double toScoreNada(double score){
		 
		 return score;
	 }

	 public double conditionalProbability(int rank) { 
		// System.out.println("prob antes :" + super.conditionalProbability(rank));
		 if(rank == 0)
			 return toScoreSigmoid(super.conditionalProbability(rank));
		 else
			 return toScoreSigmoid(super.conditionalProbability(rank))- Double.MIN_VALUE; 
	
		// return toScoreNada(super.conditionalProbability(rank));
		 
	 }
	 public static MyJointClassification calibrate ( JointClassification jc ) {
			Map<String,Double> map = new HashMap<String,Double>();
		        List<String> categories = new ArrayList<String>();	
			for ( int i = 0; i < jc.size(); i++ ) {
				categories.add(jc.category(i));
				map.put( jc.category(i), jc.jointLog2Probability(i) );
			}
		        double scores1[] = new double[categories.size()];
			for ( int i = 0; i < scores1.length; i++ ) {
				double score = map.get(categories.get(i));
				scores1[i] = score;
			}
			return new MyJointClassification(categories.toArray(new String[0]), scores1); 
	}

}
 
